package com.wen.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wen.mapper.InStoreMapper;
import com.wen.mapper.StoreMapper;
import com.wen.pojo.CurrentUser;
import com.wen.pojo.InStore;
import com.wen.pojo.Product;
import com.wen.pojo.Purchase;
import com.wen.utils.PageBean;
import com.wen.utils.Result;
import com.wen.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.wen.mapper.PurchaseMapper;
import com.wen.service.PurchaseService;

import java.util.List;

@Service
public class PurchaseServiceImpl implements PurchaseService{

    @Autowired
    private PurchaseMapper purchaseMapper;

    //查询采购单
    @Override
    public Result addCh(Purchase purchase) {
        int i = purchaseMapper.addCh(purchase);
        if(i>0){
            return Result.ok(200,"采购成功",null);
        }else{
            return Result.fail(1,"采购失败");
        }
    }
    //分页查询
    @Override
    public PageBean<Purchase> allPurchaseList(Integer pageNum, Integer pageSize, Purchase purchase) {
        PageBean<Purchase> pb = new PageBean<>();
        PageHelper.startPage(pageNum,pageSize);
        List<Purchase> list = purchaseMapper.allPurchaseList(purchase);
        Page<Purchase> p = (Page<Purchase>) list;
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return pb;
    }

    //生成采购单
    @Override
    public Result delBuyList(Integer buyId) {
        int i = purchaseMapper.delBuyList(buyId);
        if(i>0){
            return Result.ok(200,"成功",null);
        }else{
            return Result.fail(1,"失败");
        }
    }

    //修改采购单
    @Override
    public Result updBuyList(Purchase purchase) {
        int i = purchaseMapper.updBuyList(purchase);
        if(i>0){
            return Result.ok(200,"成功",null);
        }else{
            return Result.fail(1,"失败");
        }
    }

    //生成入库单
    @Autowired
    private InStoreMapper inStoreMapper;
    @Override
    public Result addInWare(Purchase purchase) {
        InStore i = new InStore();
        i.setStoreId(purchase.getStoreId());
        i.setProductId(purchase.getProductId());
        i.setInNum(purchase.getFactBuyNum());
        CurrentUser currentUser = ThreadLocalUtil.get();
        int userId = currentUser.getUserId();
        i.setCreateBy(userId);


        int i1 = inStoreMapper.addInWare(i);


        if(i1>0){
            purchaseMapper.upBuyIsIn(purchase);
            return Result.ok(200,"成功",null);
        }else{
            return Result.fail(1,"失败");
        }
    }
}
